package com.gxuwz.endrskyweather;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.gxuwz.endrskyweather.adapter.ForecastAdapter;
import com.gxuwz.endrskyweather.viewmodel.WeatherForecastViewModel;

public class Weather15dFragment extends Fragment {
    private static final String ARG_CITY_NAME = "city_name";
    private static final String ARG_CITY_CODE = "city_code";
    private static final String TAG = "Weather15dFragment";
    private WeatherForecastViewModel viewModel;
    private boolean isObserverRegistered;

    public static Weather15dFragment newInstance(String cityName, String cityCode) {
        Weather15dFragment fragment = new Weather15dFragment();
        Bundle args = new Bundle();
        args.putString(ARG_CITY_NAME, cityName);
        args.putString(ARG_CITY_CODE, cityCode);
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater,
                           @Nullable ViewGroup container,
                           @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.weather_15d, container, false);
        viewModel = new ViewModelProvider(requireParentFragment()).get(WeatherForecastViewModel.class);
        isObserverRegistered = false;
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);   
        // Initialize RecyclerView
        RecyclerView recyclerView = view.findViewById(R.id.weather_list_15d);
        recyclerView.setLayoutManager(new LinearLayoutManager(requireContext()));
        ForecastAdapter adapter = new ForecastAdapter();
        recyclerView.setAdapter(adapter);
        
        // Observe 15-day forecast data only once
        if (!isObserverRegistered) {
            viewModel.getDaily15dForecast().observe(getViewLifecycleOwner(), forecast -> {
                if (forecast != null && !forecast.isEmpty()) {
                    try {
                        adapter.updateData(forecast);
                    } catch (Exception e) {
                        Toast.makeText(requireContext(), "数据加载失败", Toast.LENGTH_SHORT).show();
                        Log.e(TAG, "Error loading 15 day forecast data: ", e);
                    }
                } else {
                    Toast.makeText(requireContext(), "无法获取天气预报数据", Toast.LENGTH_SHORT).show();
                }
            });
            isObserverRegistered = true;
        }

        // Load 15d forecast data
        String cityCode = getArguments() != null ? getArguments().getString(ARG_CITY_CODE) : "";
        viewModel.loadDaily15dForecast(cityCode);

    }
}
